﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NFLDriver;

using System.Windows.Forms;

namespace WcfServiceTransLayer
{
    
    public class DriverLogic
    {
        //some variables
        DataAccessCommonClass dataAccess = new DataAccessCommonClass();
        Teams teams;
        public DriverLogic()
        {
            populateTeams();
        }


        public void populateTeams()
        {
            Teams tempTeams;
            tempTeams = dataAccess.getTeams();
            if (tempTeams != null)
            {
                teams = dataAccess.getGetMatchResultsAll(tempTeams);
            }
            else
            {
                MessageBox.Show("Teams null at DriverLogic.populateTeams", "Null Reference", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        public User login(User userCredentials)
        {
            User databaseUserRecord;
            //just in case
            userCredentials.setLogIn(2);

            //also just in case
            User tempUser;
            databaseUserRecord = dataAccess.getUser(userCredentials.Email);
            if (databaseUserRecord.Email != null)
            {
                if(databaseUserRecord.Equals(userCredentials.Email))
                {
                    //login successful, so populate all of the values for the object with data from the database and return it
                    tempUser = dataAccess.getSelectedTeams(userCredentials.Email, userCredentials.Password);
                    //populate the selected teams with team objects that contain the selected teams Ids
                    for (int i = 0; i < 7; i++)
                    {
                        databaseUserRecord.setSelectedTeam(i, tempUser.getSelectedTeam(i));
                    }


                    //crudely search through the teams object and populate the selected teams info from the teams object
                    for (int j = 0; j < 7; j++)
                    {
                        for (int k = 0; k < 32; k++)
                        {
                            if (databaseUserRecord.getSelectedTeam(j).teamId.Equals(teams.getTeam(k).teamId))
                            {
                                databaseUserRecord.getSelectedTeam(j).teamName = teams.getTeam(k).teamName;
                                databaseUserRecord.getSelectedTeam(j).teamColumn = teams.getTeam(k).teamColumn;
                                //another for loop, sloppy, I know, but I need to do something
                                for (int l = 0; l < 17; l++)
                                {
                                    databaseUserRecord.getSelectedTeam(j).setScore(l, teams.getTeam(k).getScore(l));
                                }
                            }
                        }
                    }

                    databaseUserRecord.setLogIn(1);
                    
                    return databaseUserRecord;
                }


            }
            return userCredentials;

        }

        public void updateUserSelectedTeams(User UserValues)
        {
            dataAccess.setUserSelectedTeams(UserValues.Email, UserValues.getSelectedTeam(0).teamName, UserValues.getSelectedTeam(1).teamName, UserValues.getSelectedTeam(2).teamName, UserValues.getSelectedTeam(3).teamName, UserValues.getSelectedTeam(4).teamName, UserValues.getSelectedTeam(5).teamName, UserValues.getSelectedTeam(6).teamName);

        }

        public void createUserAccount(User userValues)
        {
            dataAccess.creatAccount(userValues.Email, userValues.Password, userValues.FirstName, userValues.LastName, "testvalue");
        }

        public Teams getTeams()
        {
            return teams;
        }
    }
}